package com.example.administrator.kotiln.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.kotiln.Path;
import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.bean.MoreGoods_Data;
import com.example.administrator.kotiln.utils.SPUtils;
import com.example.administrator.kotiln.utils.Share;
import com.example.administrator.kotiln.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by bj on 2017/3/14.
 */

public class MoreGoods_ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<MoreGoods_Data.DataBean> list;
    private int num = 1;    //商品数量
    private int submits;
    private int amounts;
    private String itemid;

    public MoreGoods_ListViewAdapter(Context context, List<MoreGoods_Data.DataBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {

            holder = new MoreGoods_ListViewAdapter.ViewHolder();
            convertView = convertView.inflate(context, R.layout.moregoods_item, null);
            holder.goods_name = (TextView) convertView.findViewById(R.id.goods_name);
            holder.goods_area = (TextView) convertView.findViewById(R.id.goods_area);
            holder.goods_Specification = (TextView) convertView.findViewById(R.id.goods_Specification);
            holder.goods_price = (TextView) convertView.findViewById(R.id.goods_price);
            holder.goodsdetail_iv_delate = (RelativeLayout) convertView.findViewById(R.id.goodsdetail_iv_delate);
            holder.goodsdetail_iv_add = (RelativeLayout) convertView.findViewById(R.id.goodsdetail_iv_add);
            holder.goodsdetail_et_num = (EditText) convertView.findViewById(R.id.goodsdetail_et_num);
            holder.shop_car = (Button) convertView.findViewById(R.id.shop_car);


            convertView.setTag(holder);
        } else {
            holder = (MoreGoods_ListViewAdapter.ViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            holder.goods_name.setText(list.get(position).getTitle());
            holder.goods_area.setText(list.get(position).getCompany());
            holder.goods_Specification.setText(list.get(position).getV1());
            String token = (String) SPUtils.get(context, "token", "");

            //登录显示的是进货价，不登录是采购价
            if (token.length() > 0 && token != null) {
                holder.goods_price.setText(list.get(position).getPrice());
            } else {
                holder.goods_price.setText(list.get(position).getMarket_price());
            }

            holder.goodsdetail_et_num.setText("1");

            //数量减的按钮
            holder.goodsdetail_iv_delate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String temp = holder.goodsdetail_et_num.getText().toString().trim();
                    if (!temp.equals("") && !temp.equals(null))
                        num = Integer.parseInt(temp);
                    if (num > 1)
                        num--;
                    holder.goodsdetail_et_num.setText(String.valueOf(num));

                }
            });
            //数量加的按钮
            holder.goodsdetail_iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String temp = holder.goodsdetail_et_num.getText().toString().trim();
                    if (!temp.equals("") && !temp.equals(null))
                        num = Integer.parseInt(temp);
                    num++;

                    holder.goodsdetail_et_num.setText(String.valueOf(num));

                }
            });
            holder.shop_car.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String submit = holder.goodsdetail_et_num.getText().toString().trim();
                    Share.d("////" + submit);
                    ToastUtils.showToast(context, submit + "");
                    if (submit.length() > 0 && submit != null) {
                        submits = Integer.parseInt(submit);
                    }

                    String amount = list.get(position).getAmount();
                    if (amount.length() > 0 && amount != null) {
                        amounts = Integer.parseInt(amount);
                    }

                    if (submits < amounts) {
                        ToastUtils.showToast(context, submits + "");
                        itemid = list.get(position).getItemid();

                        //加入购物车
                        //Addtocart();

                    } else {
                        ToastUtils.showToast(context, "只有" + amounts + "件商品啦！");
                    }

                }
            });

        }

        return convertView;
    }



    public final class ViewHolder {

        public TextView goods_name;
        public TextView goods_area;
        public TextView goods_Specification;
        public TextView goods_price;
        public RelativeLayout goodsdetail_iv_delate;
        public RelativeLayout goodsdetail_iv_add;
        public EditText goodsdetail_et_num;
        public Button shop_car;

    }
}
